var express = require("express");
var router = express.Router();
const NavModel = require("../models/NavModel");
const { findNavList } = require("../controller/navs");
const { findOneSite } = require("../controller/site");
var {
  findNewsByCategoryId,
  findNewsById,
  findNewsCategoryList,
} = require("../controller/news/findNews");
const {
  findGoodsCategoryList,
  findGoodsByCategoryId,
  findGoodsList,
  findGoodsDetailList,
  findGoodsDetailId,
} = require("../controller/goods/findGoods");

const { SuccessRes } = require("../res-model");
router.get("/", async function (req, res, next) {
  const navs = await findNavList();
  const site = await findOneSite();
  const goodsCategory = await findGoodsCategoryList();
  const goodsList = await findGoodsList();
  const hotGoodsList = new SuccessRes(
    goodsList.data.filter((item) => {
      return item.isHot;
    })
  );
  const recommandList = new SuccessRes(
    goodsList.data.filter((item) => {
      return item.isRecommend;
    })
  );
  const customerCaseList = new SuccessRes(
    goodsList.data.filter((item) => {
      return item.isCustomerCase;
    })
  );
  console.log("hotGoodsList", hotGoodsList);
  res.render("index", {
    title:
      "金属缠绕垫片_纤维石墨盘根_环氧玻璃鳞片胶泥厂家-廊坊涂鑫防腐材料有限公司",
    navs,
    site,
    goodsCategory,
    hotGoodsList,
    recommandList,
    customerCaseList,
  });
});

router.get("/about/", async function (req, res, next) {
  const navs = await findNavList();
  const site = await findOneSite();
  res.render("about/index", {
    title: "关于我们-上海济兴能源环保技术有限公司-廊坊涂鑫防腐材料有限公司",
    navs,
    site,
  });
});

router.get("/case", async function (req, res, next) {
  const navs = await findNavList();
  const site = await findOneSite();
  const goodsCategorys = await findGoodsCategoryList();
  const goods = await findGoodsByCategoryId(goodsCategorys.data[0]._id);
  res.render("case/index", {
    title: "产品展示-廊坊涂鑫防腐材料有限公司",
    navs,
    site,
    goods,
    goodsCategorys,
  });
});

router.get("/case/:id", async function (req, res, next) {
  const navs = await findNavList();
  const site = await findOneSite();
  const { id } = req.params;
  const goods = await findGoodsByCategoryId(id);
  const goodsCategorys = await findGoodsCategoryList();
  console.log("goodsCategorys", goodsCategorys);
  res.render("case/index", {
    title: "产品展示-廊坊涂鑫防腐材料有限公司",
    navs,
    site,
    goods,
    goodsCategorys,
  });
});

router.get("/case/detail/:id", async function (req, res, next) {
  const navs = await findNavList();
  const site = await findOneSite();
  let {id}=req.params;
  console.log("id", id);
  const goods = await findGoodsDetailList(id);
  const goodsCategorys = await findGoodsCategoryList();
  console.log("goods", goods);
  res.render("case/case_detail", {
    title: "产品展示-廊坊涂鑫防腐材料有限公司",
    navs,
    site,
    goods,
    goodsCategorys
  });
});

router.get("/solution/", async function (req, res, next) {
  const navs = await findNavList();
  const site = await findOneSite();
  res.render("solution/index", {
    title: "客户案例-廊坊涂鑫防腐材料有限公司",
    navs,
    site,
  });
});

router.get("/news/", async function (req, res, next) {
  const navs = await findNavList();
  const site = await findOneSite();
  const news = await findNewsByCategoryId();
  const newsCategory = await findNewsCategoryList();
  console.log("currentCategory", news.data[0].cId);
  res.render("news/index", {
    title: "公司新闻-廊坊涂鑫防腐材料有限公司",
    navs,
    site,
    news,
    currentCategory: news.data[0].cId,
    newsCategory,
  });
});

router.get("/news/:id", async function (req, res, next) {
  const navs = await findNavList();
  const site = await findOneSite();
  const { id } = req.params;
  const news = await findNewsByCategoryId(id);
  const newsCategory = await findNewsCategoryList();
  res.render("news/index", {
    title: "公司新闻-廊坊涂鑫防腐材料有限公司",
    navs,
    site,
    news,
    currentCategory: id,
    newsCategory,
  });
});
/**
 * 新闻详情页
 */
router.get("/news/details/:id", async function (req, res, next) {
  const navs = await findNavList();
  const site = await findOneSite();
  const newsCategory = await findNewsCategoryList();
  // 新闻id
  const { id } = req.params;
  const newsDetail = await findNewsById(id);

  console.log(req.params);
  res.render("news/news_details", {
    navs,
    site,
    newsDetail,
    currentCategory: newsDetail.data.cId,
    newsCategory,
  });
});
router.get("/contact/", async function (req, res, next) {
  const navs = await findNavList();
  const site = await findOneSite();
  res.render("contact/index", {
    title: "联系我们-廊坊涂鑫防腐材料有限公司",
    navs,
    site,
  });
});
router.get("/gaodemap/", async function (req, res, next) {
  res.render("gaodemap", {});
});

// 测试mongodb连接
router.get("/api/db-check", async (ctx) => {
  // 测试 mongodb 连接
  let mongodbConn;
  try {
    mongodbConn = true;
    await NavModel.findOne();
  } catch (ex) {
    mongodbConn = false;
  }

  res.json({
    errno: 0,
    data: {
      name: "express-work server",
      version: packageInfo.version,
      mongodbConn,
    },
  });
});

module.exports = router;
